package cn.wolfcode.trip.base.service.impl;

import cn.wolfcode.trip.base.domain.StrategyContent;
import cn.wolfcode.trip.base.domain.StrategyDetail;
import cn.wolfcode.trip.base.mapper.StrategyContentMapper;
import cn.wolfcode.trip.base.mapper.StrategyDetailMapper;
import cn.wolfcode.trip.base.query.QueryObject;
import cn.wolfcode.trip.base.service.IStrategyDetailService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * @author by mycp
 * @Classname StrategyDetailServiceImpl
 * @Description TODO
 * @Date 2018/11/8 18:11
 */
@Service
public class StrategyDetailServiceImpl implements IStrategyDetailService {
    @Autowired
    private StrategyDetailMapper strategyDetailMapper;
    @Autowired
    private StrategyContentMapper strategyContentMapper;

    @Override
    public PageInfo query(QueryObject qo) {
        // 根据发布时间倒序查询, 最近更新的攻略显示在最前面
        PageHelper.startPage(qo.getCurrentPage(), qo.getPageSize(), qo.getOrderBy());
        List<StrategyDetail> list = strategyDetailMapper.queryForList(qo);
        return new PageInfo(list);
    }

    @Override
    public void saveOrUpdate(StrategyDetail strategyDetail) {
        // 判断攻略文章是否有排序, 如果没有, 需要手动设置
        if (strategyDetail.getSequence() == null) {
            // 先查询出文章对应的攻略类别的最大排序
            Integer sequence = strategyDetailMapper
                    .selectSequenceByStrategyCatalogId(strategyDetail.getStrategyCatalog().getId());
            // 如果当前攻略没有最大排序, 则手动设置为0
            if (sequence == null) {
                sequence = 0;
            }
            // 设置当前攻略文章的排序为最大值 + 1
            strategyDetail.setSequence(sequence + 1);
        }

        // 设置一个时间用于设置发布时间 或 创建时间
        Date date = new Date();
        // 发布状态的文章, 需要设置发布时间 状态: 0
        if (StrategyDetail.STATE_RELEASE.equals(strategyDetail.getState())){
            strategyDetail.setReleaseTime(date);
        }

        // 判断是新增攻略文章操作 or 修改攻略文章操作
        if (strategyDetail.getId() == null) {
            // 新增操作
            // 需要设置创建时间
            strategyDetail.setCreateTime(date);
            // 先保存攻略文章
            strategyDetailMapper.insert(strategyDetail);
            // 设置主键关联, 保存攻略文章内容
            StrategyContent strategyContent = strategyDetail.getStrategyContent();
            strategyContent.setId(strategyDetail.getId());
            strategyContentMapper.insert(strategyContent);

        } else {
            // 更新操作
            // 先更新攻略文章
            strategyDetailMapper.updateByPrimaryKey(strategyDetail);
            // 设置主键关联, 同步更新攻略文章的内容
            StrategyContent strategyContent = strategyDetail.getStrategyContent();
            strategyContent.setId(strategyDetail.getId());
            strategyContentMapper.updateByPrimaryKey(strategyContent);
        }

        date = null;
    }

    @Override
    public StrategyContent getContentById(Long id) {
        return strategyContentMapper.selectByPrimaryKey(id);
    }

    @Override
    public StrategyDetail get(Long id) {
        // 查询文章
        StrategyDetail strategyDetail = strategyDetailMapper.selectByPrimaryKey(id);
        // 查询文章内容
        StrategyContent content = strategyContentMapper.selectByPrimaryKey(id);
        // 将内容设置到文章中
        strategyDetail.setStrategyContent(content);
        return strategyDetail;
    }
}
